<p>注意：本文内容适用于 Tmux 2.3 及以上的版本，但是绝大部分的特性低版本也都适用，鼠标支持、VI 模式、插件管理在低版本可能会与本文不兼容。</p>
<h1 id="tmux-快捷键-速查表-简明教程">Tmux 快捷键 &amp; 速查表 &amp; 简明教程</h1>
<p>启动新会话：</p>
<pre><code class="language-bash">tmux [new -s 会话名 -n 窗口名]</code></pre>
<p>恢复会话：</p>
<pre><code class="language-bash">tmux at [-t 会话名]</code></pre>
<p>列出所有会话：</p>
<pre><code class="language-bash">tmux ls</code></pre>
<p>关闭会话：</p>
<pre><code class="language-bash">tmux kill-session -t 会话名</code></pre>
<p>关闭所有会话：</p>
<pre><code class="language-bash">tmux ls | grep : | cut -d. -f1 | awk &#39;{print substr($1, 0, length($1)-1)}&#39; | xargs kill</code></pre>
<h1 id="在-tmux-中按下-tmux-前缀-ctrlb然后">在 Tmux 中，按下 Tmux 前缀 <code>ctrl+b</code>，然后：</h1>
<h2 id="会话">会话</h2>
<pre><code class="language-bash">:new&lt;回车&gt;  启动新会话
s           列出所有会话
$           重命名当前会话</code></pre>
<h2 id="窗口-标签页">窗口 (标签页)</h2>
<pre><code class="language-bash">c  创建新窗口
w  列出所有窗口
n  后一个窗口
p  前一个窗口
f  查找窗口
,  重命名当前窗口
&amp;  关闭当前窗口</code></pre>
<h2 id="调整窗口排序">调整窗口排序</h2>
<pre><code class="language-bash">swap-window -s 3 -t 1  交换 3 号和 1 号窗口
swap-window -t 1       交换当前和 1 号窗口
move-window -t 1       移动当前窗口到 1 号</code></pre>
<h2 id="窗格分割窗口">窗格（分割窗口）</h2>
<pre><code class="language-bash">%  垂直分割
&quot;  水平分割
o  交换窗格
x  关闭窗格
⍽  左边这个符号代表空格键 - 切换布局
q 显示每个窗格是第几个，当数字出现的时候按数字几就选中第几个窗格
{ 与上一个窗格交换位置
} 与下一个窗格交换位置
z 切换窗格最大化/最小化</code></pre>
<h2 id="同步窗格">同步窗格</h2>
<p>这么做可以切换到想要的窗口，输入 Tmux 前缀和一个冒号呼出命令提示行，然后输入：</p>
<pre><code class="language-bash">:setw synchronize-panes</code></pre>
<p>你可以指定开或关，否则重复执行命令会在两者间切换。 这个选项值针对某个窗口有效，不会影响别的会话和窗口。 完事儿之后再次执行命令来关闭。<a href="http://blog.sanctum.geek.nz/sync-tmux-panes/">帮助</a></p>
<h2 id="调整窗格尺寸">调整窗格尺寸</h2>
<p>如果你不喜欢默认布局，可以重调窗格的尺寸。虽然这很容易实现，但一般不需要这么干。这几个命令用来调整窗格：</p>
<pre><code class="language-bash">PREFIX : resize-pane -D          当前窗格向下扩大 1 格
PREFIX : resize-pane -U          当前窗格向上扩大 1 格
PREFIX : resize-pane -L          当前窗格向左扩大 1 格
PREFIX : resize-pane -R          当前窗格向右扩大 1 格
PREFIX : resize-pane -D 20       当前窗格向下扩大 20 格
PREFIX : resize-pane -t 2 -L 20  编号为 2 的窗格向左扩大 20 格</code></pre>
<h2 id="文本复制模式">文本复制模式：</h2>
<p>按下 <code>PREFIX-[</code> 进入文本复制模式。可以使用方向键在屏幕中移动光标。默认情况下，方向键是启用的。在配置文件中启用 Vim 键盘布局来切换窗口、调整窗格大小。Tmux 也支持 Vi 模式。要是想启用 Vi 模式，只需要把下面这一行添加到 .tmux.conf 中：</p>
<pre><code class="language-bash">setw -g mode-keys vi</code></pre>
<p>启用这条配置后，就可以使用 h、j、k、l 来移动光标了。</p>
<p>想要退出文本复制模式的话，按下回车键就可以了。然后按下 <code>PREFIX-]</code> 粘贴刚才复制的文本。</p>
<p>一次移动一格效率低下，在 Vi 模式启用的情况下，可以辅助一些别的快捷键高效工作。</p>
<p>例如，可以使用 w 键逐词移动，使用 b 键逐词回退。使用 f 键加上任意字符跳转到当前行第一次出现该字符的位置，使用 F 键达到相反的效果。</p>
<pre><code class="language-bash">vi             emacs        功能
^              M-m          反缩进
Escape         C-g          清除选定内容
Enter          M-w          复制选定内容
j              Down         光标下移
h              Left         光标左移
l              Right        光标右移
L                           光标移到尾行
M              M-r          光标移到中间行
H              M-R          光标移到首行
k              Up           光标上移
d              C-u          删除整行
D              C-k          删除到行末
$              C-e          移到行尾
:              g            前往指定行
C-d            M-Down       向下滚动半屏
C-u            M-Up         向上滚动半屏
C-f            Page down    下一页
w              M-f          下一个词
p              C-y          粘贴
C-b            Page up      上一页
b              M-b          上一个词
q              Escape       退出
C-Down or J    C-Down       向下翻
C-Up or K      C-Up         向下翻
n              n            继续搜索
?              C-r          向前搜索
/              C-s          向后搜索
0              C-a          移到行首
Space          C-Space      开始选中
               C-t          字符调序</code></pre>
<h2 id="杂项">杂项：</h2>
<pre><code class="language-bash">d  退出 tmux（tmux 仍在后台运行）
t  窗口中央显示一个数字时钟
?  列出所有快捷键
:  命令提示符</code></pre>
<h2 id="配置选项">配置选项：</h2>
<pre><code class="language-bash"># 鼠标支持 - 设置为 on 来启用鼠标(与 2.1 之前的版本有区别，请自行查阅 man page)
* set -g mouse on

# 设置默认终端模式为 256color
set -g default-terminal &quot;screen-256color&quot;

# 启用活动警告
setw -g monitor-activity on
set -g visual-activity on

# 居中窗口列表
set -g status-justify centre

# 最大化/恢复窗格
unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp
unbind Down
bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp</code></pre>
<h2 id="参考配置文件.tmux.conf">参考配置文件（~/.tmux.conf）：</h2>
<p>下面这份配置是我使用 Tmux 几年来逐渐精简后的配置，请自取。</p>
<pre><code class="language-bash"># -----------------------------------------------------------------------------
# Tmux 基本配置 - 要求 Tmux &gt;= 2.3
# 如果不想使用插件，只需要将此节的内容写入 ~/.tmux.conf 即可
# -----------------------------------------------------------------------------

# C-b 和 VIM 冲突，修改 Prefix 组合键为 Control-Z，按键距离近
set -g prefix C-z

set -g base-index         1     # 窗口编号从 1 开始计数
set -g display-panes-time 10000 # PREFIX-Q 显示编号的驻留时长，单位 ms
set -g mouse              on    # 开启鼠标
set -g pane-base-index    1     # 窗格编号从 1 开始计数
set -g renumber-windows   on    # 关掉某个窗口后，编号重排

setw -g allow-rename      off   # 禁止活动进程修改窗口名
setw -g automatic-rename  off   # 禁止自动命名新窗口
setw -g mode-keys         vi    # 进入复制模式的时候使用 vi 键位（默认是 EMACS）

# -----------------------------------------------------------------------------
# 使用插件 - via tpm
#   1. 执行 git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
#   2. 执行 bash ~/.tmux/plugins/tpm/bin/install_plugins
# -----------------------------------------------------------------------------

setenv -g TMUX_PLUGIN_MANAGER_PATH &#39;~/.tmux/plugins&#39;

# 推荐的插件（请去每个插件的仓库下读一读使用教程）
set -g @plugin &#39;seebi/tmux-colors-solarized&#39;
set -g @plugin &#39;tmux-plugins/tmux-pain-control&#39;
set -g @plugin &#39;tmux-plugins/tmux-prefix-highlight&#39;
set -g @plugin &#39;tmux-plugins/tmux-resurrect&#39;
set -g @plugin &#39;tmux-plugins/tmux-sensible&#39;
set -g @plugin &#39;tmux-plugins/tmux-yank&#39;
set -g @plugin &#39;tmux-plugins/tpm&#39;

# tmux-resurrect
set -g @resurrect-dir &#39;~/.tmux/resurrect&#39;

# tmux-prefix-highlight
set -g status-right &#39;#{prefix_highlight} #H | %a %Y-%m-%d %H:%M&#39;
set -g @prefix_highlight_show_copy_mode &#39;on&#39;
set -g @prefix_highlight_copy_mode_attr &#39;fg=white,bg=blue&#39;

# 初始化 TPM 插件管理器 (放在配置文件的最后)
run &#39;~/.tmux/plugins/tpm/tpm&#39;

# -----------------------------------------------------------------------------
# 结束
# -----------------------------------------------------------------------------</code></pre>
<hr />
<p>start new:</p>
<pre><code class="language-bash">tmux</code></pre>
<p>start new with session name:</p>
<pre><code class="language-bash">tmux new -s myname</code></pre>
<p>attach:</p>
<pre><code class="language-bash">tmux a  #  (or at, or attach)</code></pre>
<p>attach to named:</p>
<pre><code class="language-bash">tmux a -t myname</code></pre>
<p>list sessions:</p>
<pre><code class="language-bash">tmux ls</code></pre>
<p>kill session:</p>
<pre><code class="language-bash">tmux kill-session -t myname</code></pre>
<p>Kill all the tmux sessions:</p>
<pre><code class="language-bash">tmux ls | grep : | cut -d. -f1 | awk &#39;{print substr($1, 0, length($1)-1)}&#39; | xargs kill</code></pre>
<p>In tmux, hit the prefix <code>ctrl+b</code> (my modified prefix is ctrl+a) and then:</p>
<h2 id="list-all-shortcuts">List all shortcuts</h2>
<p>to see all the shortcuts keys in tmux simply use the <code>bind-key ?</code> in my case that would be <code>CTRL-B ?</code></p>
<h2 id="sessions">Sessions</h2>
<pre><code class="language-bash">:new&lt;CR&gt;  new session
s  list sessions
$  name session</code></pre>
<h2 id="windows-tabs">Windows (tabs)</h2>
<pre><code class="language-bash">c  create window
w  list windows
n  next window
p  previous window
f  find window
,  name window
&amp;  kill window</code></pre>
<h2 id="panes-splits">Panes (splits)</h2>
<pre><code class="language-bash">%  vertical split
&quot;  horizontal split

o  swap panes
q  show pane numbers
x  kill pane
+  break pane into window (e.g. to select text by mouse to copy)
-  restore pane from window
⍽  space - toggle between layouts
&lt;prefix&gt; q (Show pane numbers, when the numbers show up type the key to goto that pane)
&lt;prefix&gt; { (Move the current pane left)
&lt;prefix&gt; } (Move the current pane right)
&lt;prefix&gt; z toggle pane zoom</code></pre>
<h2 id="sync-panes">Sync Panes</h2>
<p>You can do this by switching to the appropriate window, typing your Tmux prefix (commonly Ctrl-B or Ctrl-A) and then a colon to bring up a Tmux command line, and typing:</p>
<pre><code class="language-bash">:setw synchronize-panes</code></pre>
<p>You can optionally add on or off to specify which state you want; otherwise the option is simply toggled. This option is specific to one window, so it won’t change the way your other sessions or windows operate. When you’re done, toggle it off again by repeating the command. <a href="http://blog.sanctum.geek.nz/sync-tmux-panes/">tip source</a></p>
<h2 id="resizing-panes">Resizing Panes</h2>
<p>You can also resize panes if you don’t like the layout defaults. I personally rarely need to do this, though it’s handy to know how. Here is the basic syntax to resize panes:</p>
<pre><code class="language-bash">PREFIX : resize-pane -D (Resizes the current pane down)
PREFIX : resize-pane -U (Resizes the current pane upward)
PREFIX : resize-pane -L (Resizes the current pane left)
PREFIX : resize-pane -R (Resizes the current pane right)
PREFIX : resize-pane -D 20 (Resizes the current pane down by 20 cells)
PREFIX : resize-pane -U 20 (Resizes the current pane upward by 20 cells)
PREFIX : resize-pane -L 20 (Resizes the current pane left by 20 cells)
PREFIX : resize-pane -R 20 (Resizes the current pane right by 20 cells)
PREFIX : resize-pane -t 2 20 (Resizes the pane with the id of 2 down by 20 cells)
PREFIX : resize-pane -t -L 20 (Resizes the pane with the id of 2 left by 20 cells)</code></pre>
<h2 id="copy-mode">Copy mode:</h2>
<p>Pressing PREFIX [ places us in Copy mode. We can then use our movement keys to move our cursor around the screen. By default, the arrow keys work. we set our configuration file to use Vim keys for moving between windows and resizing panes so we wouldn’t have to take our hands off the home row. tmux has a vi mode for working with the buffer as well. To enable it, add this line to .tmux.conf:</p>
<pre><code class="language-bash">setw -g mode-keys vi</code></pre>
<p>With this option set, we can use h, j, k, and l to move around our buffer.</p>
<p>To get out of Copy mode, we just press the ENTER key. Moving around one character at a time isn’t very efficient. Since we enabled vi mode, we can also use some other visible shortcuts to move around the buffer.</p>
<p>For example, we can use “w” to jump to the next word and “b” to jump back one word. And we can use “f”, followed by any character, to jump to that character on the same line, and “F” to jump backwards on the line.</p>
<pre><code class="language-bash">   Function                vi             emacs
   Back to indentation     ^              M-m
   Clear selection         Escape         C-g
   Copy selection          Enter          M-w
   Cursor down             j              Down
   Cursor left             h              Left
   Cursor right            l              Right
   Cursor to bottom line   L
   Cursor to middle line   M              M-r
   Cursor to top line      H              M-R
   Cursor up               k              Up
   Delete entire line      d              C-u
   Delete to end of line   D              C-k
   End of line             $              C-e
   Goto line               :              g
   Half page down          C-d            M-Down
   Half page up            C-u            M-Up
   Next page               C-f            Page down
   Next word               w              M-f
   Paste buffer            p              C-y
   Previous page           C-b            Page up
   Previous word           b              M-b
   Quit mode               q              Escape
   Scroll down             C-Down or J    C-Down
   Scroll up               C-Up or K      C-Up
   Search again            n              n
   Search backward         ?              C-r
   Search forward          /              C-s
   Start of line           0              C-a
   Start selection         Space          C-Space
   Transpose chars                        C-t</code></pre>
<h2 id="misc">Misc</h2>
<pre><code class="language-bash">d  detach
t  big clock
?  list shortcuts
:  prompt</code></pre>
<h2 id="configurations-options">Configurations Options:</h2>
<pre><code class="language-bash"># Mouse support - set to on if you want to use the mouse
* setw -g mode-mouse off
* set -g mouse-select-pane off
* set -g mouse-resize-pane off
* set -g mouse-select-window off

# Set the default terminal mode to 256color mode
set -g default-terminal &quot;screen-256color&quot;

# enable activity alerts
setw -g monitor-activity on
set -g visual-activity on

# Center the window list
set -g status-justify centre

# Maximize and restore a pane
unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp
unbind Down
bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp</code></pre>
<h2 id="resources">Resources:</h2>
<ul>
<li><a href="http://pragprog.com/book/bhtmux/tmux">tmux: Productive Mouse-Free Development</a></li>
<li><a href="http://superuser.com/questions/343572/tmux-how-do-i-reorder-my-windows">How to reorder windows</a></li>
</ul>
<h2 id="notes">Notes:</h2>
<ul>
<li></li>
</ul>
<h2 id="changelog">Changelog:</h2>
<ul>
<li>1411143833002 - Added <a href="#PanesSplits">toggle zoom</a> under Panes (splits) section.</li>
<li>1411143833002 - <a href="#syncPanes">Added Sync Panes</a></li>
<li>1414276652677 - <a href="#killAllSessions">Added Kill all tmux sessions</a></li>
<li>1438585211173 - <span class="citation" data-cites="justinjhendrick">[corrected create and add next and previus thanks to @justinjhendrick]</span>(#WindowsTabs)</li>
</ul>
<h2 id="request-an-update">Request an Update:</h2>
<p>We Noticed that our Cheatsheet is growing and people are coloberating to add new tips and tricks, so please tweet to me what would you like to add and let’s make it better!</p>
<ul>
<li>Twitter: <span class="citation" data-cites="MohammedAlaa">[@MohammedAlaa]</span>(http://twitter.com/MohammedAlaa)</li>
</ul>
